<?php
namespace Rainrock\Framework\kernel\base;


use Rainrock\Framework\kernel\db\DB;
class Base{
	
	private static $configInfo = array();
	
	/**
	*	开始运行
	*	@param $path 系统跟路径
	*/
	public static function runStart($path, $package, $packpath)
	{
		@session_start();
		if(function_exists('date_default_timezone_set'))date_default_timezone_set('Asia/Shanghai'); //设置默认时区
		header('Content-Type:text/html;charset=utf-8');
		$path = str_replace('\\','/', $path);
		define('ROOT_PATH',  $path); //根目录
		define('ROOT_PATM',  substr($path, 0, strrpos($path, '/')));//根目录上一个目录
		define('HOST', (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''));
	
		if(!$package)$package 	= 'app';
		if(!$packpath)$packpath = 'application';
		
		$config= array(
			'debug'  	=> false,
			'urly'	 	=> 'http://www.rockoa.com/', //官网地址
			'title'	 	=> 'rockrain',
			'updir'  	=> 'upload',
			'version'  	=> '1.0',
			'qom'	 	=> 'rock_',
			'highpass'	=> '', //超级管理密码
			'url'	 	=> '',
			'urlout'	=> '', //外部地址
			'urlstatic'	=> '', //默认前端样式地址(正常不用配置)
			'urllocal'	=> '', //本地地址如http://127.0.0.1:8090/
			'package'	=> $package,	//主包名
			'packpath'	=> $packpath,	//项目的目录
			'admintable'=> 'users',	//管理登录用的表
			'db_type'	=> 'mysqli',
			'db_host'	=> '127.0.0.1',
			'db_user'	=> 'root',
			'db_pass'	=> '666666',
			'apptheme'	=> '#009966', //主题颜色
			'db_base'	=> '',
			'db_perfix' => '', 
			'db_encode' => 'utf8mb4',
			'randkey'   => 'ulgsijanexhkdzrbqcmyofvtwp',	
			'phppath'   => 'php',
			'loginmode' => '0',	//登录模式，0默认帐号密码,1必须短信验证码,2微信验证码
			'adminurl'	=> '?m=adminmanage'
		);
		define('PACKAGE',  $config['package']);
		define('PACKPATH',  $config['packpath']);
		define('ROOT_PACK',  ROOT_PATM.'/'.PACKPATH);//包名全部路径
		
		$_path = ''.ROOT_PACK.'/config/config.php';
		if(file_exists($_path)){
			$_tempconf	= require($_path);
			foreach($_tempconf as $_tkey=>$_tvs)$config[$_tkey] = $_tvs;
		}
		
		$_path = ''.ROOT_PACK.'/config/config_local.php';
		if(file_exists($_path)){
			$_tempconf	= require($_path);
			foreach($_tempconf as $_tkey=>$_tvs)$config[$_tkey] = $_tvs;
		}
		
		$_path = ''.ROOT_PACK.'/config/version.php';
		if(file_exists($_path))$config['version'] = require($_path);
		
		
		define('DEBUG',  $config['debug']);
		define('URLY',  $config['urly']);
		define('UPDIR',  $config['updir']);
		define('QOM',  $config['qom']);
		define('TITLE',  $config['title']);
		define('VERSION',  $config['version']);
		define('URLSTATIC',  $config['urlstatic']);
		$url 			= $config['url'];
		if(!$url)$url 	= Rock::url();
		define('URL',  $url);
		define('URLN',  Rock::url());
		define('ISHTTPS',  (substr(URLN,0,5)=='https'));
		define('URLLOCAL',  (($config['urllocal']) ? $config['urllocal'] : URL));
		
		$apptheme 	= Request::get('apptheme');
		if(strlen($apptheme)==6)Cookie::set('apptheme', $apptheme);
		if(!$apptheme)$apptheme = Cookie::get('apptheme');
		if(strlen($apptheme)==6)$config['apptheme'] = '#'.$apptheme.'';
		define('APPTHEME',  $config['apptheme']);
		
		self::$configInfo = $config;
		error_reporting(DEBUG ? E_ALL : 0);
	}
	
	/**
	*	获取配置参数
	*/
	public static function getConfig($key, $dev='')
	{
		$val = $dev;
		if(isset(self::$configInfo[$key]))$val = self::$configInfo[$key];
		return $val;
	}
	
	/**
	*	运行结束
	*/
	public static function runEnd()
	{
		DB::closeDb();
	}
}